﻿-- =============================================
-- Author: tien dung
-- Create date: 26-07-2012
-- Description:	xu ly table Item
-- =============================================
Create PROCEDURE [dbo].[ups_item]
	@flag		int, -- 0: SEF, 1: INS, 2: DEL, 3: UPD, 4: SEL
	@id			int =-1,
	@code		varchar(100),
	@name		nvarchar(100)='',
	@journey_code	varchar(100)='',
	@airline_code	varchar(100)='',
	@type			int=-1,
	@purchase_price	decimal=0,
	@purchase_commision	decimal=0,
	@sale_price	decimal=0,
	@refund_fee	decimal=0,
	@return_fee	decimal=0,
	@deleted	int=-1,
	@error int output,
	@error_message nvarchar(200) output
AS
BEGIN
	set @error=0
	set @error_message='OK'

	if @flag = 0 -- select full
    begin
        select * From Item
    end
    else if @flag = 1 -- insert
	begin
		begin try
			insert Item ([code],[name],[journey_code],[airline_code],[type],[purchase_price],[purchase_commision],[sale_price],[refund_fee],[return_fee],[deleted])
			values		(@code,@name,@journey_code,@airline_code,@type,@purchase_price,@purchase_commision,@sale_price,@refund_fee,@return_fee,@deleted)
		end try
		
		begin catch
			SELECT @error=ERROR_NUMBER() , @error_message=isnull(ERROR_MESSAGE(),'')
		end catch	
    end
	else if @flag = 2 -- delete
	begin
		begin try
			delete Item
			where [code]=@Code
		end try
		begin catch
			SELECT @error=ERROR_NUMBER() ,@error_message=isnull(ERROR_MESSAGE(),'')
		end catch	
    end
	else if @flag = 3 --update
	begin
		begin try
			update Item
			set 
				[code]=@code,
				[name]=@name,
				[journey_code] = @journey_code,
				[airline_code] = @airline_code,
				[type] = @type,
				[purchase_price] = @purchase_price,
				[purchase_commision] = @purchase_commision,
				[sale_price]= @sale_price,
				[refund_fee] = @refund_fee,
				[return_fee] = @return_fee,
				[deleted] = @deleted
			where [code]=@Code
		end try
		begin catch
			SELECT @error=ERROR_NUMBER() ,@error_message=isnull(ERROR_MESSAGE(),'')
		end catch	
    end
	else if @flag = 4 -- selecr with Where condition
	begin
		select * From Item
		where [code]=@Code
    end

END


